package com.wallet.admin.controller.system;


import com.wallet.admin.feign.system.SysUserFeignClient;
import com.wallet.common.util.PageUtil;
import com.wallet.common.util.StringUtil;
import com.wallet.common.vo.ApiResult;
import com.wallet.common.vo.DataResult;
import com.wallet.common.vo.PageResult;
import com.wallet.provider.model.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author: HaijunLiu
 * @email: isaiah@sohu.com
 * @description：系统管理
 * @date: 2018/1/30 21:09
 * @mofified By: 
 * @version 1.0
 */
@Controller
@RequestMapping("/system/sysUser")
public class SysUserController {

    @Autowired
    SysUserFeignClient sysUserFeignClient;

    /**
     * 分页查询系统用户
     * @param offset
     * @param limit
     * @param order
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public DataResult<SysUser> list(@RequestParam(value = "search",required = false) String search, @RequestParam(defaultValue = "1") int offset, @RequestParam(defaultValue = "10")  int limit, @RequestParam(defaultValue = "createTime")String ordername,  @RequestParam(defaultValue = "desc") String order){
        PageResult<SysUser> users = sysUserFeignClient.getSysUserList(search, PageUtil.getPageNum(offset, limit), limit, StringUtil.camel2Underline(ordername), order);
        return new DataResult.Builder().build(users);
    }

    /**
     * 验证登录名是否已存在
     * @param userId
     * @return
     */
    @RequestMapping("/validUserId")
    @ResponseBody
    public ApiResult validUserId(String userId){
        boolean rs = sysUserFeignClient.validUserId(userId);
        if (rs){
            return new ApiResult.Builder().success(rs).build();
        }else {
            return new ApiResult.Builder().failure().build();
        }

    }
    /**
     * 添加系统用户
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public ApiResult add(@RequestBody  SysUser sysUser){
        //sysUser.setCreateBy(super.getCurrentUserId());
        boolean rs = sysUserFeignClient.addSysUser(sysUser);
        return new ApiResult.Builder().success(rs).build();
    }

    /**
     * 删除系统用户
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ApiResult delete(String ids){
        boolean rs = sysUserFeignClient.deleteSysUser(ids);
        return new ApiResult.Builder().success(rs).build();
    }

    /**
     * 跳到修改页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(Long id, Model model){
        SysUser sysUser = sysUserFeignClient.getSysUserById(id);
        model.addAttribute("sysUser", sysUser);
        return "system/sys_user_edit";
    }
    /**
     * 修改系统用户
     * @param sysUser
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public ApiResult update(SysUser sysUser){
        Boolean rs = sysUserFeignClient.updateSysUser(sysUser);
        return new ApiResult.Builder().success(rs).build();
    }


}
